package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;

public class FarmerNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.farmer.com.cn/.*/[0-9]*/t[0-9]*_[0-9]*.htm",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				if(url.indexOf("/pic/")!=-1 || url.indexOf("/video/")!=-1){
					return false;
				}
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		NewsMeta news=new NewsMeta();

		doc.select("[style*=display: none;]").remove();
		doc.select(".newslist-conts-w").remove();
		title=doc.select(".title a").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("h1.wtitle").text();
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("td[height=25]").text();
		}

		date=JSoupUtils.matchDate(doc, "日期：","来源：");
		doc.select("div#div").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select("div.TRS_Editor"));

		/*
		 * 作者
		 */
		author =JSoupUtils.matchAuthor(doc, "来源：");
		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
//		String url="http://www.farmer.com.cn/snsx/xwbb/xwbbzx/201405/t20140505_958898.htm";
//		FarmerNewsAnalyse test=new FarmerNewsAnalyse();
//		url=UrlArgumentTop.FromatUrl(url);
//		System.out.println(url);
//		System.out.println(DoMainUtils.GetDomainName(url));
//		UrlMeta meta=CrawlHTML.responseToURL(url);
//		NewsMeta parserHtml=test.parserHtml(meta);
//		System.out.println(parserHtml);
		long parseLong=Long.parseLong("9816431222849507");
		System.out.println(parseLong);
	}
	
	

	
	public boolean isNeedUpdate(){
		return false;
	}
}
